
#include "precompiled.h"
#include "windowsmanager.h"

#include <QApplication>

//#include <gui/scene.h>
#include "gui/window.h"
#include "gui/qmlcontrol.h"

#include "helper.h"
#include "configstack.h"
#include "engine.h"

#include <extensions/mainwindow.h>
#include <extensions/zoneinterface.h>

#include <QtDeclarative>

#include "ui/desktopwidget.h"

class WindowsManagerPrivate{
	public:
	WindowsManagerPrivate():
			windowsDisplayMode(WindowsManager::on_scene)
			,desktopWidget(0)
	{}
		~WindowsManagerPrivate()
		{
			delete desktopWidget;
			QMapIterator<QString, Control*> i(controls);
			while (i.hasNext()){
				i.next();
				delete i.value();
			}

			controls.clear();
		}

		QGraphicsScene* currentScene()
		{
			return scenes.first();
		}

		WindowsManager::WindowsDisplayMode windowsDisplayMode;
		QList<QGraphicsScene*> scenes;

		QMap<QString, Control*> controls;
		QMap<Optra::Extensions::MainWindow*, QGraphicsProxyWidget*> windowMap;
		DesktopWidget* desktopWidget;
		QSignalMapper * signalMapper;
};

WindowsManager::WindowsManager(const QString& configKey, Opl::Core::EntityObject *parent) :
    Opl::Core::EntityObject(parent), O_PRIVATE_CREATE(WindowsManager)
{    
    O_D(WindowsManager);
    LI.Info() << "Window manager initialize: " << toString();

    QString displayModeStr = conf(configKey, "mode", "on_scene").toString();

    LI.Debug() << "Setup display mode :" << displayModeStr;
    setProperty("windowsDisplayMode", displayModeStr);

}

WindowsManager::~WindowsManager()
{
	O_PRIVATE_DELETE;
}

void WindowsManager::addScene(QGraphicsScene* scene)
{
	O_D(WindowsManager);
	d->scenes.append(scene);
}

void WindowsManager::setWindowsDisplayMode(WindowsDisplayMode mode)
{
	O_D(WindowsManager);
	d->windowsDisplayMode = mode;
	QTimer::singleShot(0, this, SLOT(reposition()));
}

void WindowsManager::reposition()
{
	O_D(WindowsManager);

}

WindowsManager::WindowsDisplayMode WindowsManager::windowsDisplayMode() const
{
	O_C(WindowsManager);
	return d->windowsDisplayMode;
}

void WindowsManager::initialize()
{
	O_D(WindowsManager);
	//d->desktopWidget = new DesktopWidget(0);
	//d->desktopWidget->show();
}

void WindowsManager::removeWindows(Optra::Extensions::ZoneInterface* inf)
{
	O_D(WindowsManager);
}

void WindowsManager::addWindow(Optra::Extensions::MainWindow * window)
{
	O_D(WindowsManager);
//	QGraphicsProxyWidget *pwidget = new QGraphicsProxyWidget(0, Qt::Window);
//	pwidget->setWidget(window);
	d->windowMap.insert(window, d->currentScene()->addWidget(window, Qt::Dialog));
	window->move(100, 200);
	window->show();
}

void WindowsManager::removeWindow(Optra::Extensions::MainWindow* window)
{
	O_D(WindowsManager);
	d->currentScene()->removeItem(d->windowMap.value(window));
	d->windowMap.remove(window);

}

QObject* WindowsManager::ObjectFromQMLCommponent(QDeclarativeComponent* commponent)
{
	return 0;
}

void WindowsManager::addItem(QDeclarativeItem* item)
{
	O_D(WindowsManager);
	item->moveToThread(QApplication::instance()->thread());
	d->currentScene()->addItem(item);
}

void WindowsManager::removeItem(QDeclarativeItem* item)
{
	O_D(WindowsManager);
	d->currentScene()->removeItem(item);
}

bool WindowsManager::registerQMLControl(const QString& className, const QString& script)
{
	O_D(WindowsManager);
	if (d->controls.contains(className)){
		LI.Warning() << "WindowsManager::registerQMLControl " << className << " already exists";
		return false;
	}

	QmlControl* control = new QmlControl;
	control->load(className, QUrl::fromLocalFile(script));
	d->controls.insert(className, control);
	return true;

}
